//## 作业1:使用open write close的形式去创建一个大文件,内容从指定的文本中随机取一些（每次取得长度内容可以不一样）,计算出这个大文件出现最多的字符(要使用流的形式).
//创建一个大文件.读取出最多的字符
let fs = require("fs");
if (!fs.existsSync("bigdata")) fs.mkdirSync("bigdata");
let str = "qwertyuiopasdfghjklzxcvbnm";
let startAt = new Date().getTime();
let fd = fs.openSync("./bigdata/big.txt", "a");
for (let i = 0; i < 1000000; i++) {
   fs.writeSync(fd, str.slice(Math.floor(Math.random() * (str.length - 1))), str.length - 1);
}
fs.closeSync(fd);
let endAt = new Date().getTime();
console.log(endAt - startAt);
//读取最多的字符
//
let chatArray = [];
let readStream = fs.createReadStream("./bigdata/big.txt");
readStream.on("data", (chunk) => {
   let str = chunk.toString();
   for (let i = 0; i < str.length; i++) {
      if (chatArray[str[i]] == undefined) {
         chatArray[str[i]] = 1;
      } else {
         chatArray[str[i]] += 1;
      }
   }
});

readStream.on("end", () => {
   console.log(chatArray);
   //怎么取最大值,如果还没概念，三四节课继续写写
})